using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using PHSRAG.Insight21.Reports;
using System.Collections;
using System.Text;
using System.Data;

namespace PHSRAG.Insight21.Agreements.Reports
{

    /// <remarks>Class for generating Financial Summary report</remarks>
    public class sqlAgreementSummaryReport:sqlReport
    {
        /// <summary>
        /// constant report name
        /// </summary>
        private const string cReportName = "Agmt_AgreementSummary";
        public const string cReportTitle = "Agreement Summary";
        private const string SearchCriteriaVersionKey = "SearchCriteriaVersion_27447545-6976-4179-A0A7-B3AFED489466";

        public sqlAgreementSummaryReport(string pageUrl):base(pageUrl)
        {
            doHashtable = true;
            //doCustomExcelReport = true;

            //pUrl.bLoadParameter = false;
            //pUser.bLoadParameter = false;

        }
    
        public override string ReportName
        {
            get
            {
                return cReportName;
            }
 
        }


        /// <summary>
        /// over ride parent method
        /// </summary>
        public override Hashtable setParameterHTable(ref string rptName)
        {
            rptName = ReportName;

            reportTitle = cReportTitle;

            Agreements.AgreementSummaries agreementSummaries = AgreementSummaries;
            reportSource = agreementSummaries.GetDataContainer();

            reportCriteria = AddRefreshDatesToCriteria();


            return base.setParameterHTable(ref rptName);
        }

        /// <summary>
        /// Get the Agreements (summary of agreements) that the logged-in user has access to. The list of agreements is
        /// is maintained by the Agreements business object within its data container. The Agreements object is cached.
        /// </summary>
        private AgreementSummaries AgreementSummaries
        {
            get
            {
                int searchVersion = searchCriteria.Version;

                AgreementSummaries agreementSummaries = Policy.ResultsCache.GetObject(cachePolicy, AgreementSummaries.CacheKey) as AgreementSummaries;
                if ((agreementSummaries == null) || (searchVersion != SearchCriteriaVersion))
                {
                    SearchCriteriaVersion = searchVersion;

                    Policy.ResultsCache.PurgeObject(cachePolicy);

                    agreementSummaries = new AgreementSummaries();
                    agreementSummaries.Search(searchCriteria, Session.SessionID);

                    Policy.ResultsCache.SetObject(cachePolicy, AgreementSummaries.CacheKey, agreementSummaries);
                }
                return agreementSummaries;
            }
        }

        /// <summary>
        /// Gets/sets an integer value indicating the search criteria version. 
        /// The version number changes when the search criteria changes.  This signals the page to re-build its
        /// cache.
        /// </summary>
        /// <remarks>
        /// This and All properties required to re-establish the page are stored as name / value pairs in
        /// a hash table stored in the session cache under the key Keys.FinancialSummaryPageKey.  
        /// This enables the page to re-establish its state with minimal IO to what it was when
        /// the user last visited it.
        /// </remarks>
        private int SearchCriteriaVersion
        {
            get
            {
                Hashtable h = pageCachePolicy.GetCachedObject(Keys.AgreementSummaryPageKey) as Hashtable;
                return (h == null || h[SearchCriteriaVersionKey] == null) ? 0 : (int)h[SearchCriteriaVersionKey];
            }
            set
            {
                Hashtable h = pageCachePolicy.GetCachedObject(Keys.AgreementSummaryPageKey) as Hashtable;
                if (h == null)
                    h = new Hashtable();

                h[SearchCriteriaVersionKey] = value;
                pageCachePolicy.Cache(Keys.AgreementSummaryPageKey, h);
            }
        }
    }

}